[Solved] How to convert int timestamp to DateTime in 您所在的位置:网站首页 Timestamp Converter [Solved] How to convert int timestamp to DateTime in

[Solved] How to convert int timestamp to DateTime in

2022-12-17 17:45| 来源: 网络整理| 查看: 265

How to convert int timestamp to DateTime in json_serializable flutter json dart flutter 5,142 Solution 1

To convert an int timestamp to DateTime, you need to pass a static method that returns a DateTime result to the fromJson parameter in the @JsonKey annotation.

This code solves the problem and allows the convertion.

@JsonSerializable(nullable: false) class Person { final String firstName; final String lastName; @JsonKey(fromJson: _fromJson, toJson: _toJson) final DateTime dateOfBirth; Person({this.firstName, this.lastName, this.dateOfBirth}); factory Person.fromJson(Map json) => _$PersonFromJson(json); Map toJson() => _$PersonToJson(this); static DateTime _fromJson(int int) => DateTime.fromMillisecondsSinceEpoch(int); static int _toJson(DateTime time) => time.millisecondsSinceEpoch; }

usage

Person person = Person.fromJson(json.decode('{"firstName":"Ada", "lastName":"Amaka", "dateOfBirth": 1553456553132 }')); Solution 2

I use this:

@JsonSerializable() class Person { @JsonKey(fromJson: dateTimeFromTimestamp) DateTime dateOfBirth; ... } DateTime dateTimeFromTimestamp(Timestamp timestamp) { return timestamp == null ? null : timestamp.toDate(); } Share: 5,142 nonybrighto Author by nonybrighto

Updated on December 10, 2022 Comments nonybrighto nonybrighto 30 minutes

How can I convert an integer timestamp to Datetime.

Sample Code:

@JsonSerializable(nullable: false) class Person { final String firstName; final String lastName; final DateTime dateOfBirth; Person({this.firstName, this.lastName, this.dateOfBirth}); factory Person.fromJson(Map json) => _$PersonFromJson(json); Map toJson() => _$PersonToJson(this); }

How do I convert dateOfBirth integer timeStamp to DateTime?

EngineSense EngineSense almost 2 years It didn't work for me in my case data from firebase, Has the timestamp value to datetime(local) failed. Recents Flutter - Pass state change function to child widget and update state Flutter FetchData from API and save local with sqflite Need help in making a reusable widget in flutter How does Flutter Widget rerender works when it's properties change? Distance between two Offsets in Flutter? Flutter local notification onSelectNotification can not open a new page when a local notification is clicked Running flutter apps on multiple simulators Listen to Events instead of States with Flutter BLOC why LogCat and AVD Manager missing in Android Studio when flutter project is opened How to pass parameters to flutter web app firebase auth undefined class 'OAuthProvider' Flutter iOS - MediaQuery.of(context).size.width does not give the width in pixel


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有